import axios from 'axios';
import qs from 'qs';
import {getConfig} from "../config";


axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded';
axios.defaults.withCredentials = true

axios.interceptors.request.use((config) => {

    return config;
})
axios.interceptors.response.use((response) => {

    if (response.data) {
        return response.data
    } else {
        return response;
    }
}, (error) => {
    console.log(error);

    return Promise.reject(error);
})

export function post(url, data) {
    if (process.env.NODE_ENV === 'development') {
        // dev
        let rootUrl = getConfig().rootUrl
        url = url.replace(rootUrl,"/api")
    } else {
        // build
    }
    return axios.post(url, qs.stringify(data));
}

export function get(url, params) {
    if (process.env.NODE_ENV === 'development') {
        // dev
        let rootUrl = getConfig().rootUrl
        url = url.replace(rootUrl,"/api/")
    } else {
        // build
    }
    return axios.get(url, {params});
}